package manage.action;

import com.alibaba.fastjson.JSONObject;
import m.common.action.ActionMeta;
import m.common.action.RequestMeta;
import m.common.service.HostInfoService;
import m.system.cache.CacheHost;
import m.system.cache.CacheMap;
import m.system.cache.CacheMap2;
import m.system.cache.CacheMapList;
import m.system.url.UrlMarker;
import m.system.util.HttpRequestUtil;
import m.system.util.JSONMessage;
import m.system.util.StringUtil;

import java.util.HashMap;
import java.util.Map;

@ActionMeta(name="manage/hostInfo")
public class HostInfoAction extends ManageAction {
	@RequestMeta(params= {"ipport","method","fieldOid","param"})
	public JSONMessage operHost(String ipport,String method,String fieldOid,String param){
		JSONMessage message=new JSONMessage();
		try {
			verifyAdminOperPower("manage_system_power");
			getService(HostInfoService.class).operHost(ipport,method,fieldOid,param);
			fillJSONResult(message,true,"请求已发送");
		} catch (Exception e) {
			fillJSONResult(message,false,e);
		}
		return message;
	}
	public JSONMessage toList(){
		JSONMessage message=new JSONMessage();
		try {
			verifyAdminOperPower("manage_system_power");
			message.push("list", getService(HostInfoService.class).getList());
			message.push("host", HostInfoService.getCurrentHost());
			fillJSONResult(message,true,"");
		} catch (Exception e) {
			fillJSONResult(message,false,e);
		}
		return message;
	}
	@RequestMeta(params= {"urlHost"})
	public JSONMessage getCacheList(String urlHost) {
		JSONMessage message=new JSONMessage();
		try {
			verifyAdminOperPower("manage_system_power");
			if(!StringUtil.isSpace(urlHost)) {
				Map<String,String> header= new HashMap<>();
				header.put("Authorization", UrlMarker.getAuth()+" "+getSessionAdmin().getToken());
				JSONObject json=JSONObject.parseObject(new HttpRequestUtil().doPost("http://"+urlHost+"action/manage/hostInfo/getCacheList", "", header));
				message.push("model",json.get("model"));
				message.push("cacheHost",json.get("cacheHost"));
				message.push("cacheMap",json.get("cacheMap"));
				message.push("cacheMap2",json.get("cacheMap2"));
				message.push("cacheList",json.get("cacheList"));
			}else {
				message.push("model", HostInfoService.getCurrentHost());
				message.push("cacheHost", CacheHost.getAll());
				message.push("cacheMap", CacheMap.getAll());
				message.push("cacheMap2", CacheMap2.getAll());
				message.push("cacheList", CacheMapList.getAll());
			}
			fillJSONResult(message,true,"");
		} catch (Exception e) {
			fillJSONResult(message,false,e);
		}
		return message;
	}
	

}
